package main

import (
	"fmt"
	"reflect"
	"runtime"
)

func eval(a, b int, op string) (int, error) {
	switch op {
	case "+":
		return a + b, nil
	case "-":
		return a - b, nil
	case "*":
		return a * b, nil
	case "/":
		return a / b, nil
	default:
		return 0, fmt.Errorf("undef %s", op)
	}
}

func div(a, b int) (int, int) {
	return a / b, a % b
}

func div2(a, b int) (q, r int) {
	return a / b, a % b
}

func div3(a, b int) (q, r int) {
	q = a / b
	r = a % b
	return
}

func apply(op func(int, int) int, a, b int) int {
	p := reflect.ValueOf(op).Pointer()
	opName := runtime.FuncForPC(p).Name()
	fmt.Printf("Calling funciton %s with args"+
		"(%d, %d)", opName, a, b)
	return op(a, b)
}

func pow(a, b int) int {
	return a * b
}

func sum(numbers ...int) int{
	s :=0
	for i :=range numbers{
		s += i
	}
	return s
}

func main() {
	value, err := eval(1, 2, "+")
	if err != nil {
		panic(fmt.Sprintf(
			"%s", err,
		))
	}
	fmt.Println(value, err)
	q, r := div2(3, 2)
	fmt.Println(div(3, 2))
	fmt.Println(q, r)

	//只接受某个变量
	g, _ := div(4, 5)
	fmt.Println(g)

	//匿名函数默认将代码块的内容以引用的方式传递
	fmt.Println(apply(func(a, b int) int {
		return a * b
	}, 1, 2))

	fmt.Println(apply(pow, 1, 2))

	fmt.Println(sum(1,2,3,4,5))
}
